package tools
{
	import mx.controls.Alert;
	import mx.controls.Button;
	
	import nl.wv.extenders.panel.SuperPanel;
	public class Tool
	{
		[Embed(source="../blue_plastic.swf", symbol="Panel_borderSkin")]
		private static var Panel_borderSkin:Class;
		public function Tool()
		{
		}
		public function createPanel(title:String, pos:Number, w:Number, h:Number, x:Number, y:Number, sc:Boolean, er:Boolean, name:String="Default"): SuperPanel{
			var panel:SuperPanel = new SuperPanel();
			panel.backButton=new Button();
			panel.x=x;
			panel.y=y;
			panel.width=w;
			panel.height=h;
			panel.showControls=sc;
			panel.enableResize=er;
			panel.layout="absolute";
			panel.title=( title.length > 30 ? title.substr(0,30)+"..." : title);
			if(title.length>30) panel.toolTip = title;
			panel.backButton.label = ( title.length > 30 ? title.substr(0,30)+"..." : title);
			panel.backButton.toolTip = "Lijevi klik za otvaranje ili desni za zatvaranje";
			panel.position = pos;
			panel.name = name;
			return panel;				
		}
		
		public function createAlert(naslov:String, tekst:String, height:Number=150):void{
	 		Alert.buttonHeight = 50;
	 		Alert.okLabel = "Uredu";
	 		var pom:String=naslov;
	 		var pp:String="";
	 		for(var i:int = 20; i>naslov.length; i=i-1) pp+=" ";
	 		if (naslov.length < 25) naslov=pp+"."+pom+pp+".";
	 			else naslov=pom.substring(0, 25)+"...";
	 		var a:Alert = Alert.show(tekst, naslov);
	 		a.height = height;
	 		a.setStyle("borderSkin", Panel_borderSkin);
	 		a.setStyle("color", 0x000000);
		}
		
		public function checkForCRC(s:String): Boolean{
			var check:Boolean = false;
			for(var j:Number=0; j<s.length-2; j++){
				if(s.charAt(j)=="C"&&s.charAt(j+1)=="R"&&s.charAt(j+2)=="C") {
					check = true;
				}
			}
			return check;
		}
		
		public function CodeTekst(s:String): String{
			for(var i:Number=0; i<s.length; i++){
				var pom:String="";
				if(s.charAt(i)=='<') {
					pom=s.substring(i+1);
					s = s.substring(0,i)+"CRC1";
					s+=pom;
				}
				if(s.charAt(i)=='>') {
					pom=s.substring(i+1);
					s = s.substring(0,i)+"CRC2";
					s+=pom;
				}
			}
			return s;
		}
		
		public function DecodeTekst(s:String): String{
			var pattern1:RegExp = /CRC1/g;
			var pattern2:RegExp = /CRC2/g;    
 			s = s.replace(pattern1, "<");
 			s = s.replace(pattern2, ">");
 			return s;
		}
	
		public function PlainTekst(s:String): String{
			s = DecodeTekst(s);
			var write:Boolean = true;
			var pom:String = "";
			for(var i:Number=0; i<s.length; i++){
				if(s.charAt(i)=='<') write = false;
				if(write) pom+=s.charAt(i);
				if(i+3<s.length&&s.charAt(i)=="<"&&s.charAt(i+1)=="/"&&s.charAt(i+2)=="P"&&s.charAt(i+3)==">") pom+="\n";
				if(s.charAt(i)=='>') {
					write = true;
				}
			}
 			return pom;
		}
		
		public function currentDate():String{
			var d:Date = new Date();
			return d.fullYear+"-"+(d.month+1).toString()+"-"+d.date;
		}
		
		public function currentTime():String{
			var d:Date = new Date();
			return d.hours+":"+d.minutes+":"+d.seconds;
		}

		public function currentTimeMinus(n:Number):String{
			var d:Date = new Date();
			var h:Number;
			var m:Number;
			if(d.minutes-n>=0) {
				m=d.minutes-n;
				h=d.hours;
			} else{
				m = 60 + (d.minutes-n);
				if(d.hours-1>=0) h = d.hours-1; else return "00:00:00";
			}
			
			return h+":"+m+":"+d.seconds;
		}
		
		public function canOpen(s:String):Boolean{
			var array:Array = new Array("pdf", "flv", "mp3", "html", "slt");
			for(var i:Number = 0; i<array.length; i++){
				if(array[i]==s) return true;
			}			
			return false;
		}
		
		public function canDownload(s:String):Boolean{
			var array:Array = new Array("html", "slt");
			for(var i:Number = 0; i<array.length; i++){
				if(array[i]==s) return false;
			}			
			return true;
		}
		
		public function uniqueNumber():String{
			var date:Date = new Date();
			return date.fullYear+(date.month+1).toString()+date.date+date.hours+date.minutes+date.seconds+date.milliseconds;
		}
	}
}